package com.lanqiao.gxnushop.service;

import com.lanqiao.gxnushop.entity.Address;
import com.lanqiao.gxnushop.entity.Orders;
import com.lanqiao.gxnushop.mapper.OrdersMapper;
import com.lanqiao.gxnushop.util.UUIdUtil;
import com.lanqiao.gxnushop.valueObject.OrderDetails;
import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Schema(description = "订单服务")
@Service
public class OrdersService {
    @Autowired
    private AddressService addressService;
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private ProductsService productsService;
    @Autowired
    private StoresService storesService;

    /*@Schema(description = "添加订单")
    public boolean add(Orders orders) {
        if(productsService.findById(orders.getProductId())!=null&&storesService.select(orders.getStoreId())!=null){
            orders.setOrderId(UUIdUtil.getUserId());
            ordersMapper.add(orders);
            if(Integer.parseInt(productsService.findById(orders.getProductId()).getStock())-Integer.parseInt(orders.getQuantity())>0){
                productsService.updateStock(orders.getProductId(),String.valueOf(Integer.parseInt(productsService.findById(orders.getProductId()).getStock())-Integer.parseInt(orders.getQuantity())));
                return true;
            }
            return false;

        }
        return false;
    }*/

    @Schema(description = "批量添加订单")
    public boolean addOrdersBatch( ArrayList<Orders> ordersList) {
        boolean allSuccess = false;
        System.out.println("-------------------555-------------------"+ordersList);
        ArrayList<Address> adreesslist = addressService.findByUserId(ordersList.get(0).getBuyerId());
        /*String adressId = adreesslist.get(0).getAddressId();*/

        for (Orders orders : ordersList) {
            orders.setOrderId(UUIdUtil.getUserId());
            orders.setState("0");
            /*orders.setAddressId(adressId);*/
            // 检查库存
            int stock = Integer.parseInt(productsService.findById(orders.getProductId()).getStock());
            int quantity = Integer.parseInt(orders.getQuantity());
            if (stock >= quantity) {
                allSuccess = true;
                //break;
                // 更新库存
                //productsService.updateStock(orders.getProductId(), String.valueOf(stock - quantity));
                // 添加订单
                ordersMapper.add(orders);
            }




           /* if (productsService.findById(orders.getProductId()) != null && storesService.select(orders.getStoreId()) != null) {
                orders.setOrderId(UUIdUtil.getUserId());

                // 检查库存
                int stock = Integer.parseInt(productsService.findById(orders.getProductId()).getStock());
                int quantity = Integer.parseInt(orders.getQuantity());
                if (stock < quantity) {
                    allSuccess = false;
                    break;
                }
                // 更新库存
                //productsService.updateStock(orders.getProductId(), String.valueOf(stock - quantity));
                // 添加订单
                ordersMapper.add(orders);
            } else {
                allSuccess = false;
                break;
            }*/
        }

        return allSuccess;
    }

    @Schema(description = "删除订单")
    public boolean delete(String ordersId) {
        return ordersMapper.delete(ordersId);
    }



    @Schema(description = "根据买家id查询订单")
    public ArrayList<OrderDetails> findByBuyerId(String buyerId) {
        return ordersMapper.findByBuyerId(buyerId);
    }

    @Schema(description = "根据订单id查询订单")
    public Orders findById(String ordersId) {
        return ordersMapper.findById(ordersId);
    }

    @Schema(description = "根据店铺id查询订单")
    public ArrayList<Orders> findByStoreId(String storeId) {
        return ordersMapper.findByStoreId(storeId);
    }

    @Schema(description = "更新订单")
    public boolean update(Orders orders) {
        return ordersMapper.update(orders);
    }
    /*@Schema(description = "根据订单id查询商品")
    public Products findProductByOrdersId(String ordersId) {
        return ordersMapper.findProductByOrdersId(ordersId);
    }

    @Schema(description = "根据订单id查询买家")
    public Products findBuyerByOrdersId(String ordersId) {
        return ordersMapper.findBuyerByOrdersId(ordersId);
    }

    @Schema(description = "根据订单id查询店铺")
    public Products findStoreByOrdersId(String ordersId) {
        return ordersMapper.findStoreByOrdersId(ordersId);
    }*/

}
